﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.LMS;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Data;
using Apps.Models;
using System.ComponentModel;

namespace Apps.Web.Areas.LMS.Controllers
{
    public class CollectSampleController : BaseController
    { 
        [Dependency]
        public ILMS_COLLECTSAMPLEBLL m_BLL { get; set; }
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index(string id)
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList(GridPager pager, string testCode,string startDate,string endDate, string collectID, string sampleCode, string makeCode, string batchCode)
        {
            List<LMS_COLLECTSAMPLEModel> list = m_BLL.GetList(ref pager, testCode, startDate, endDate, "", collectID, sampleCode, makeCode, batchCode);
            GridRows<LMS_COLLECTSAMPLEModel> grs = new GridRows<LMS_COLLECTSAMPLEModel>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }
        #region 创建
        [SupportFilter]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Create(LMS_COLLECTSAMPLEModel model)
        {
            model.COLLECTION_ID = ResultHelper.NewId;
            if (model != null && ModelState.IsValid)
            {

                if (m_BLL.Create(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE" + model.TEST_CODE + ",SEND_USER_ID" + model.SEND_USER_ID, "成功", "创建", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE" + model.TEST_CODE + ",SEND_USER_ID" + model.SEND_USER_ID + "," + ErrorCol, "失败", "创建", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }
        #endregion

        #region 修改
        [SupportFilter]
        public ActionResult Edit(string id)
        {
            LMS_COLLECTSAMPLEModel entity = m_BLL.GetById(id);
            return View(entity);
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Edit(LMS_COLLECTSAMPLEModel model)
        {
            if (model != null && ModelState.IsValid)
            {

                if (m_BLL.Edit(ref errors, model))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE" + model.TEST_CODE + ",SEND_USER_ID" + model.SEND_USER_ID, "成功", "修改", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(1, Resource.EditSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE" + model.TEST_CODE + ",SEND_USER_ID" + model.SEND_USER_ID + "," + ErrorCol, "失败", "修改", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(0, Resource.EditFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.EditFail));
            }
        }
        #endregion

        #region 详细
        [SupportFilter]
        public ActionResult Details(string id)
        {
            LMS_COLLECTSAMPLEModel entity = m_BLL.GetById(id);
            return View(entity);
        }

        #endregion

        #region 删除
        [HttpPost]
        [SupportFilter]
        public ActionResult Delete(string id)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                if (m_BLL.Delete(ref errors, id))
                {
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE:" + id, "成功", "删除", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    LogHandler.WriteServiceLog(GetUserId(), "TEST_CODE" + id + "," + ErrorCol, "失败", "删除", "LMS_COLLECTSAMPLE");
                    return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail));
            }
        }
        #endregion
        #region 导出导入
        [HttpPost]
        [SupportFilter]
        public ActionResult Import(string filePath)
        {
            var list = new List<LMS_COLLECTSAMPLEModel>();
            bool checkResult = m_BLL.CheckImportData(Utils.GetMapPath(filePath), list, ref errors);
            //校验通过直接保存
            if (checkResult)
            {
                m_BLL.SaveImportData(list);
                LogHandler.WriteServiceLog(GetUserId(), "导入成功", "成功", "导入", "LMS_COLLECTSAMPLE");
                return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
            }
            else
            {
                string ErrorCol = errors.Error;
                LogHandler.WriteServiceLog(GetUserId(), ErrorCol, "失败", "导入", "LMS_COLLECTSAMPLE");
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
            }
        }
        [HttpPost]
        [SupportFilter(ActionName = "Export")]
        public JsonResult CheckExportData()
        {
            List<LMS_COLLECTSAMPLEModel> list = m_BLL.GetList(ref setNoPagerAscById, "");
            if (list.Count().Equals(0))
            {
                return Json(JsonHandler.CreateMessage(0, "没有可以导出的数据"));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(1, "可以导出"));
            }
        }
        [SupportFilter]
        public ActionResult Export()
        {
            List<LMS_COLLECTSAMPLEModel> list = m_BLL.GetList(ref setNoPagerAscById, "");
            JArray jObjects = new JArray();
            foreach (var item in list)
            {
                var jo = new JObject();
                jo.Add("TEST_CODE", item.TEST_CODE);
                jo.Add("SEND_USER_ID", item.SEND_USER_ID);
                jo.Add("SAMPLE_ID", item.SAMPLE_ID);
                jo.Add("PRINT_COUNT", item.PRINT_COUNT);
                jo.Add("MAKE_TYPE", item.MAKE_TYPE);
                jo.Add("MAKE_CODE", item.MAKE_CODE);
                jo.Add("COLLECT_USER_ID", item.COLLECT_USER_ID);
                jo.Add("COLLECT_TIME", item.COLLECT_TIME);
                jo.Add("COLLECT_ID", item.COLLECTION_ID);
                jo.Add("BATCH_CODE", item.BATCH_CODE);
                jObjects.Add(jo);
            }
            var dt = JsonConvert.DeserializeObject<DataTable>(jObjects.ToString());
            var exportFileName = string.Concat(
                "File",
                DateTime.Now.ToString("yyyyMMddHHmmss"),
                ".xlsx");
            return new ExportExcelResult
            {
                SheetName = "Sheet1",
                FileName = exportFileName,
                ExportData = dt
            };
        }
        #endregion
    }


}

/// <summary>
/// 样品类型
/// </summary>
[Description("样品类型")]
public enum SampleType
{
    [Description("0.2mm")]
    S2 = 0x10,
    [Description("3mm")]
    S3 = 8,
    [Description("6mm")]
    S6 = 1,

    [Description("13mm全水")]
    S13mm全水 = 20,
    [Description("1.5mmY")]
    S1_5mmY = 21,
    [Description("1mm岩相")]
    S1mm岩相 = 22,
    [Description("0.425mm流动")]
    S0_425mm流动 = 23,
    [Description("0.2mm工分")]
    S0_2mm工分 = 24,
    [Description("0.2mmb")]
    S0_2mmb = 25,
    [Description("0.2mmG")]
    S0_2mmG = 26,
    [Description("0.2mm热值")]
    S0_2mm热值 = 27,
    [Description("CSR25mm")]
    CSR25mm = 28
}
